
***************************************************************
/*
0) creates TN4 and TN5
1) creates OD density
2) creates OD focal
*/

cd "`c(pwd)'"   // Set working directory to wherever the user placed "replication_package"

* Define global paths relative to the main directory
global main_dir "`c(pwd)'"
global data_dir "$main_dir/data_analysis"
global rawdata_dir "$main_dir/data_raw"
global tables_dir "$main_dir/tables"

cd "$rawdata_dir/"


* 1) CREATE TNIC4:
use tnic3_data, clear
bys gvkey1 year:  egen pct50 = pctile(score) if score!=. , p(50)
keep if score>=pct50 & score!=.
keep year gvkey1 gvkey2 score
cd "$data_dir/"
save tnic4_data, replace
***************************************************************
** 2) OD DENSITY
use overlapping_directors, clear
rename gvkey1 temp
rename gvkey2 gvkey1
rename temp gvkey2
save overlapping_directors_transf, replace
append using overlapping_directors
save overlapping_directors_double, replace
use overlapping_directors_double, clear
drop if gvkey1==gvkey2
g overdirs=1
g overdirs2=1
g overdirs4=1
g overdirs2_4=1
capture drop _merge
joinby gvkey1 gvkey2 year using tnic2_data, unmatched(master)
drop if _merge!=3
replace overdirs2=0 if _merge!=3
drop _merge score
joinby gvkey1 gvkey2 year using tnic4_data, unmatched(master)
replace overdirs4=0 if _merge!=3
replace overdirs2_4=0 if _merge==3
drop _merge score
rename gvkey1 gvkey
drop gvkey2
collapse (sum) overdirs overdirs2  overdirs4   overdirs2_4 , by(year gvkey)
save temp_2, replace
use tnic2_data, clear
rename gvkey1 gvkey
joinby gvkey year using sample_od, unmatched(master)
drop if _merge!=3
drop _merge
rename gvkey gvkey1
rename gvkey2 gvkey
joinby gvkey year using sample_od, unmatched(master)
drop if _merge!=3
drop _merge
rename gvkey gvkey2
g tnpeers2=1
g tnpeers4=1
g tnpeers2_4=1
capture drop _merge
joinby gvkey1 gvkey2 year using tnic4_data, unmatched(master)
replace tnpeers4=0 if _merge!=3
replace tnpeers2_4=0 if _merge==3
drop _merge score
rename gvkey1 gvkey
drop gvkey2
collapse (sum) tnpeers2  tnpeers4   tnpeers2_4 , by(year gvkey)
save temp2_2, replace
use temp2_2, clear
merge year gvkey using temp_2
drop if _merge ==2
drop _merge
replace overdirs=0 if overdirs==.
replace overdirs2=0 if overdirs2==.
replace overdirs4=0 if overdirs4==.
replace overdirs2_4=0 if overdirs2_4==.
g odrat2=overdirs2/tnpeers2
g odrat4=overdirs4/tnpeers4
g odrat2_4=overdirs2_4/tnpeers2_4
sort year gvkey
save jh_overdirs2od, replace
sum
use tnic2_data, clear
drop if gvkey1==gvkey2
rename gvkey2 gvkey
sort year gvkey
joinby year gvkey using jh_overdirs2od, unmatched(master)
keep if _merge==3
drop _merge score
rename gvkey gvkey2
keep gvkey1 gvkey2 year odrat2  odrat4   odrat2_4 
sort gvkey1 gvkey2 year
joinby gvkey1 gvkey2 year using tnic4_data, unmatched(master)
replace odrat4=. if _merge!=3
replace odrat2_4=. if _merge==3
drop _merge score
rename gvkey1 gvkey
sum
collapse (mean) odrat2  odrat4   odrat2_4 , by(year gvkey)
sort year gvkey
rename odrat2 oddensity2
rename odrat4 oddensity4
rename odrat2_4 oddensity2_4
save od_density, replace
sum
********************************************************************
** 3) OD FOCAL
use jh_overdirs2od, clear
gen od_dummy=1 if overdirs>0 & overdirs!=.
gen odtn2_dummy=1 if overdirs2>0 & overdirs2!=.
gen odtn4_dummy=1 if overdirs4>0 & overdirs4!=.
gen odtn2_4_dummy=1 if overdirs2_4>0 & overdirs2_4!=.
replace od_dummy=0 if overdirs==0
replace odtn2_dummy=0 if overdirs2==0
replace odtn4_dummy=0 if overdirs4==0
replace odtn2_4_dummy=0 if overdirs2_4==0
rename odrat2 odfocal2
rename odrat4 odfocal4
rename odrat2_4 odfocal2_4
keep year gvkey odfocal2 odfocal4 odfocal2_4 od_dummy odtn2_dummy  odtn4_dummy odtn2_4_dummy 
save od_focal, replace





